﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using DataAccessLayer.DataInterfaces;
using MyCommon;

namespace DataAccessLayer
{
    public class NghiepVuDAO : INghiepVu
    {
        private static QLBaoHanh dataContext;
        public NghiepVuDAO()
        {
            dataContext = new QLBaoHanh();
        }

        public IEnumerable<View_ThietBiBHDetail> GetDsThietBiDoiBHSC()
        {
            return dataContext.View_ThietBiBHDetail.Where(o => o.TrangThaiThietBiBHID == (int)TrangThaiThietBiBH.DoiBHSC).Select(aObj => (View_ThietBiBHDetail)aObj);
        }

        public IEnumerable<View_ThietBiBHDetail> GetDsThietBiBHSCByNhanVienId(int inNhanVienId)
        {
            return dataContext.View_ThietBiBHDetail.Where(o => o.IdNhanVienThucHien == inNhanVienId ).Select(aObj => (View_ThietBiBHDetail)aObj);
        }

        public IEnumerable<View_ThietBiBHDetail> GetDsThietBiBHSCByNhanVienId(DateTime inFromDate, DateTime inToDate, int inNhanVienId)
        {
            DateTime FromDate = new DateTime(inFromDate.Year, inFromDate.Month, inFromDate.Day, 0, 0, 0);
            DateTime ToDate = new DateTime(inToDate.Year, inToDate.Month, inToDate.Day, 23, 59, 59);
            return dataContext.View_ThietBiBHDetail.Where(
                o => o.IdNhanVienThucHien == inNhanVienId &&
                     o.ThoiDiemGiaoNVThucHien >= FromDate &&
                     o.ThoiDiemGiaoNVThucHien <= ToDate).Select(aObj => (View_ThietBiBHDetail)aObj);
        }
        public IEnumerable<View_ThietBiBHDetail> GetDsThietBiBHSCByNhanVienId(DateTime inFromDate, DateTime inToDate, int inNhanVienId, int inTrangThaiThietBiId)
        {
            DateTime FromDate = new DateTime(inFromDate.Year, inFromDate.Month, inFromDate.Day, 0, 0, 0);
            DateTime ToDate = new DateTime(inToDate.Year, inToDate.Month, inToDate.Day, 23, 59, 59);
            return dataContext.View_ThietBiBHDetail.Where(
                o => o.IdNhanVienThucHien == inNhanVienId &&
                     o.TrangThaiThietBiBHID == inTrangThaiThietBiId&&
                     o.ThoiDiemGiaoNVThucHien >= FromDate &&
                     o.ThoiDiemGiaoNVThucHien <= ToDate
                     ).Select(aObj => (View_ThietBiBHDetail)aObj);
        }

        public IEnumerable<View_TheoDoiTraHang> GetDsThietBiInListTrangThai(List<int> inListTrangThaiTBId)
        {
            return dataContext.View_TheoDoiTraHang.Where(
                o => inListTrangThaiTBId.Contains(o.TrangThaiThietBiBHID.Value)).Select(aObj => (View_TheoDoiTraHang)aObj);
        }
        public IEnumerable<View_TheoDoiTraHang> GetDsThietBiInListTrangThai(List<int> inListTrangThaiTBId, DateTime inTraFromDate, DateTime inTraToDate)
        {
            DateTime FromDate = new DateTime(inTraFromDate.Year, inTraFromDate.Month, inTraFromDate.Day, 0, 0, 0);
            DateTime ToDate = new DateTime(inTraToDate.Year, inTraToDate.Month, inTraToDate.Day, 23, 59, 59);
            return dataContext.View_TheoDoiTraHang.Where(
                o => inListTrangThaiTBId.Contains(o.TrangThaiThietBiBHID.Value) &&
                    FromDate <= o.ThoiDiemTra && o.ThoiDiemTra <= ToDate
                ).Select(aObj => (View_TheoDoiTraHang)aObj);
        }

        public IEnumerable<View_TheoDoiTraHang> GetDsThietBiInListTrangThaiLocMaPhien(List<int> inListTrangThaiTBId, string inMaPhien)
        {
            return dataContext.View_TheoDoiTraHang.Where(
                o => inListTrangThaiTBId.Contains(o.TrangThaiThietBiBHID.Value) && o.SoBienNhan.Contains(inMaPhien)).Select(aObj => (View_TheoDoiTraHang)aObj);
        }
        public IEnumerable<View_TheoDoiTraHang> GetDsThietBiInListTrangThaiLocMaPhien(List<int> inListTrangThaiTBId, string inMaPhien, DateTime inTraFromDate, DateTime inTraToDate)
        {
            DateTime FromDate = new DateTime(inTraFromDate.Year, inTraFromDate.Month, inTraFromDate.Day, 0, 0, 0);
            DateTime ToDate = new DateTime(inTraToDate.Year, inTraToDate.Month, inTraToDate.Day, 23, 59, 59);
            return dataContext.View_TheoDoiTraHang.Where(
                o => inListTrangThaiTBId.Contains(o.TrangThaiThietBiBHID.Value) &&
                    o.SoBienNhan.Contains(inMaPhien) &&
                    FromDate <= o.ThoiDiemTra && o.ThoiDiemTra <= ToDate
                    ).Select(aObj => (View_TheoDoiTraHang)aObj);
        }
        
        
        public IEnumerable<View_TheoDoiTraHang> GetDsThietBiInListTrangThaiLocTenKhachHang(List<int> inListTrangThaiTBId, string inTenKhachHang)
        {
            return dataContext.View_TheoDoiTraHang.Where(
                o => inListTrangThaiTBId.Contains(o.TrangThaiThietBiBHID.Value) && o.TenKhachHang.Contains(inTenKhachHang)).Select(aObj => (View_TheoDoiTraHang)aObj);
        }
        public IEnumerable<View_TheoDoiTraHang> GetDsThietBiInListTrangThaiLocTenKhachHang(List<int> inListTrangThaiTBId, string inTenKhachHang, DateTime inTraFromDate, DateTime inTraToDate)
        {
            DateTime FromDate = new DateTime(inTraFromDate.Year, inTraFromDate.Month, inTraFromDate.Day, 0, 0, 0);
            DateTime ToDate = new DateTime(inTraToDate.Year, inTraToDate.Month, inTraToDate.Day, 23, 59, 59);
            return dataContext.View_TheoDoiTraHang.Where(
                o => inListTrangThaiTBId.Contains(o.TrangThaiThietBiBHID.Value) &&
                    o.TenKhachHang.Contains(inTenKhachHang) &&
                    FromDate <= o.ThoiDiemTra && o.ThoiDiemTra <= ToDate
                    ).Select(aObj => (View_TheoDoiTraHang)aObj);
        }

        public IEnumerable<View_TheoDoiTraHang> GetDsThietBiInListTrangThaiLocMaThietBi(List<int> inListTrangThaiTBId, string inMaThietBi)
        {
            return dataContext.View_TheoDoiTraHang.Where(
                o => inListTrangThaiTBId.Contains(o.TrangThaiThietBiBHID.Value) && o.MaThietBiBH.Contains(inMaThietBi)).Select(aObj => (View_TheoDoiTraHang)aObj);
        }
        public IEnumerable<View_TheoDoiTraHang> GetDsThietBiInListTrangThaiLocMaThietBi(List<int> inListTrangThaiTBId, string inMaThietBi, DateTime inTraFromDate, DateTime inTraToDate)
        {
            DateTime FromDate = new DateTime(inTraFromDate.Year, inTraFromDate.Month, inTraFromDate.Day, 0, 0, 0);
            DateTime ToDate = new DateTime(inTraToDate.Year, inTraToDate.Month, inTraToDate.Day, 23, 59, 59);
            return dataContext.View_TheoDoiTraHang.Where(
                o => inListTrangThaiTBId.Contains(o.TrangThaiThietBiBHID.Value) &&
                    o.MaThietBiBH.Contains(inMaThietBi) &&
                    FromDate <= o.ThoiDiemTra && o.ThoiDiemTra <= ToDate
                    ).Select(aObj => (View_TheoDoiTraHang)aObj);
        }


        public IEnumerable<View_TheoDoiTraHang> GetDsThietBiInListTrangThaiLocTenThietBi(List<int> inListTrangThaiTBId, string inTenThietBi)
        {
            return dataContext.View_TheoDoiTraHang.Where(
                o => inListTrangThaiTBId.Contains(o.TrangThaiThietBiBHID.Value) && o.TenThietBiBH.Contains(inTenThietBi)).Select(aObj => (View_TheoDoiTraHang)aObj);
        }
        public IEnumerable<View_TheoDoiTraHang> GetDsThietBiInListTrangThaiLocTenThietBi(List<int> inListTrangThaiTBId, string inTenThietBi, DateTime inTraFromDate, DateTime inTraToDate)
        {
            DateTime FromDate = new DateTime(inTraFromDate.Year, inTraFromDate.Month, inTraFromDate.Day, 0, 0, 0);
            DateTime ToDate = new DateTime(inTraToDate.Year, inTraToDate.Month, inTraToDate.Day, 23, 59, 59);
            return dataContext.View_TheoDoiTraHang.Where(
                o => inListTrangThaiTBId.Contains(o.TrangThaiThietBiBHID.Value) &&
                    o.TenThietBiBH.Contains(inTenThietBi) &&
                    FromDate <= o.ThoiDiemTra && o.ThoiDiemTra <= ToDate
                    ).Select(aObj => (View_TheoDoiTraHang)aObj);
        }

        public IEnumerable<View_PhieuTiepNhan> GetReportPhieuTiepNhanById(int inPhieuId)
        {
            return dataContext.View_PhieuTiepNhan.Where(o => o.PhienId == inPhieuId).Distinct();
        }

        public IEnumerable<View_ThietBiBHDetail> GetDsTbiDangONCCById(int NhaCungCapId = 0)
        {
            if (NhaCungCapId == 0)
            {
                return dataContext.View_ThietBiBHDetail.Where(
                o => o.TrangThaiThietBiBHID == (int)MyCommon.TrangThaiThietBiBH.DangONCC
                     ).Select(aObj => (View_ThietBiBHDetail)aObj);
            }
            else
            {
                return dataContext.View_ThietBiBHDetail.Where(
                o => o.TrangThaiThietBiBHID == (int)MyCommon.TrangThaiThietBiBH.DangONCC
                    && o.NhaCungCapId == NhaCungCapId
                     ).Select(aObj => (View_ThietBiBHDetail)aObj);
            }
        }

        public IEnumerable<View_ThietBiBHDetail> GetDsThietBiBHSCCuaNVTheoNgayThucHien(DateTime inFromDate, DateTime inToDate, int inNhanVienId, List<int> inTrangThaiList)
        {
            DateTime FromDate = new DateTime(inFromDate.Year, inFromDate.Month, inFromDate.Day, 0, 0, 0);
            DateTime ToDate = new DateTime(inToDate.Year, inToDate.Month, inToDate.Day, 23, 59, 59);
            return dataContext.View_ThietBiBHDetail.Where(
                o => o.IdNhanVienThucHien == inNhanVienId &&
                     inTrangThaiList.Contains((int)o.TrangThaiThietBiBHID)&&
                     o.ThoiDiemGiaoNVThucHien >= FromDate &&
                     o.ThoiDiemGiaoNVThucHien <= ToDate).Select(aObj => (View_ThietBiBHDetail)aObj).OrderBy(avc => avc.TrangThaiThietBiBHID);
        }
        //public IEnumerable<View_ThietBiBHDetail> GetDsThietBiBHSCDaHoanThanhByNhanVienId(DateTime inFromDate, DateTime inToDate, int inNhanVienId)
        //{
        //    DateTime FromDate = new DateTime(inFromDate.Year, inFromDate.Month, inFromDate.Day, 0, 0, 0);
        //    DateTime ToDate = new DateTime(inToDate.Year, inToDate.Month, inToDate.Day, 23, 59, 59);
        //    return dataContext.View_ThietBiBHDetail.Where(
        //        o => o.IdNhanVienThucHien == inNhanVienId &&
        //             o.TrangThaiThietBiBHID != (int)MyCommon.TrangThaiThietBiBH.DangOPhongKT &&
        //             o.ThoiDiemGiaoNVThucHien >= FromDate &&
        //             o.ThoiDiemGiaoNVThucHien <= ToDate
        //             ).Select(aObj => (View_ThietBiBHDetail)aObj);
        //}


        public IEnumerable<View_PhienBHSCNgay> GetDsPhienBHSCNgay(DateTime inTiepNhanFromDate, DateTime inTiepNhanToDate)
        {
            DateTime FromDate = new DateTime(inTiepNhanFromDate.Year, inTiepNhanFromDate.Month, inTiepNhanFromDate.Day, 0, 0, 0);
            DateTime ToDate = new DateTime(inTiepNhanToDate.Year, inTiepNhanToDate.Month, inTiepNhanToDate.Day, 23, 59, 59);
            return dataContext.View_PhienBHSCNgay.Where(
                o => FromDate <= o.ThoiGianNhan && o.ThoiGianNhan <= ToDate
                ).Select(aObj => (View_PhienBHSCNgay)aObj);
        }

        public IEnumerable<View_TheoDoiTraHang> GetDsPhienTiepNhanBHSC(DateTime inTiepNhanFromDate, DateTime inTiepNhanToDate)
        {
            DateTime FromDate = new DateTime(inTiepNhanFromDate.Year, inTiepNhanFromDate.Month, inTiepNhanFromDate.Day, 0, 0, 0);
            DateTime ToDate = new DateTime(inTiepNhanToDate.Year, inTiepNhanToDate.Month, inTiepNhanToDate.Day, 23, 59, 59);
            return dataContext.View_TheoDoiTraHang.Where(
                o => FromDate <= o.ThoiGianNhan && o.ThoiGianNhan <= ToDate
                ).Select(aObj => (View_TheoDoiTraHang)aObj);
        }

        public IEnumerable<View_TheoDoiTraHang> GetDsPhienKhongDungHan(DateTime inTiepNhanFromDate, DateTime inTiepNhanToDate)
        {
            DateTime FromDate = new DateTime(inTiepNhanFromDate.Year, inTiepNhanFromDate.Month, inTiepNhanFromDate.Day, 0, 0, 0);
            DateTime ToDate = new DateTime(inTiepNhanToDate.Year, inTiepNhanToDate.Month, inTiepNhanToDate.Day, 23, 59, 59);
            return dataContext.View_TheoDoiTraHang.Where(
                o => FromDate <= o.ThoiGianNhan && o.ThoiGianNhan <= ToDate
                && o.NgayHenTra < o.ThoiDiemTra
                ).Select(aObj => (View_TheoDoiTraHang)aObj);
        }
    }
}
